package top.tupobi.zing.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.StringUtils;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import top.tupobi.zing.R;
import top.tupobi.zing.listener.OnCheckFriendApplyCallback;
import top.tupobi.zing.listener.OnFriendApplyResult;
import top.tupobi.zing.mvp.model.FriendApply;
import top.tupobi.zing.network.HttpConfig;

public class AdapterRvFriendApplyList extends RecyclerView.Adapter<AdapterRvFriendApplyList.ItemViewHolder>{

    private List<FriendApply> mInvites = new ArrayList<>();

    private Context mContext;
    private OnFriendApplyResult mOnFriendApplyResult;

    public AdapterRvFriendApplyList(Context context) {
        mContext = context;
    }

    public void setOnFriendApplyResult(OnFriendApplyResult onFriendApplyResult) {
        mOnFriendApplyResult = onFriendApplyResult;
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ItemViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_of_rv_friend_apply_list, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
        String fromAvatarUrl = mInvites.get(position).getInviteFromAvatarUrl();
        String nickname = mInvites.get(position).getInviteFromNickname();
        String applyDate = mInvites.get(position).getInviteStartTime();
        String fromUsername = mInvites.get(position).getInviteFromUsername();
        int status = mInvites.get(position).getInviteStatus();

        if (!StringUtils.isEmpty(fromAvatarUrl)) {
            Glide.with(mContext).load(HttpConfig.PIC_ROOT + fromAvatarUrl).into(holder.cim_avatar);
        }
        if (StringUtils.isEmpty(nickname)) {
            holder.tv_from_nickname.setText("暂无昵称");
        } else {
            holder.tv_from_nickname.setText(nickname);
        }
        holder.tv_apply_date.setText(applyDate);
        holder.tv_from_username.setText(fromUsername);

        if (status == 0) {
            // 申请中
            holder.ll_status.setVisibility(View.GONE);
            holder.ll_check.setVisibility(View.VISIBLE);
            holder.tv_check_through.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnFriendApplyResult != null) {
                        mOnFriendApplyResult.onApplyThrough(mInvites.get(position), new OnCheckFriendApplyCallback() {
                            @Override
                            public void operateResult(boolean isOk) {
                                if (isOk) {
                                    mInvites.get(position).setInviteStatus(1);
                                    notifyItemChanged(position);
                                }
                            }
                        });
                    }
                }
            });
            holder.tv_check_reject.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnFriendApplyResult != null) {
                        mOnFriendApplyResult.onApplyReject(mInvites.get(position).getInviteId(), new OnCheckFriendApplyCallback() {
                            @Override
                            public void operateResult(boolean isOk) {
                                mInvites.get(position).setInviteStatus(2);
                                notifyItemChanged(position);
                            }
                        });
                    }
                }
            });
        } else if (status == 1) {
            // 通过
            holder.ll_status.setVisibility(View.VISIBLE);
            holder.ll_check.setVisibility(View.GONE);
            holder.tv_status.setText("已通过");
        } else if ((status == 2)) {
            // 拒绝
            holder.ll_status.setVisibility(View.VISIBLE);
            holder.ll_check.setVisibility(View.GONE);
            holder.tv_status.setText("已拒绝");
        }
    }

    @Override
    public int getItemCount() {
        return mInvites.size();
    }

    public void notifyApplyListChanged(List<FriendApply> invites) {
        this.mInvites = invites;
        notifyDataSetChanged();
    }

    static class ItemViewHolder extends RecyclerView.ViewHolder {
        private CircleImageView cim_avatar;
        private TextView tv_from_nickname, tv_apply_date, tv_from_username, tv_status, tv_check_through, tv_check_reject;
        private LinearLayout ll_status, ll_check;

        ItemViewHolder(@NonNull View itemView) {
            super(itemView);
            cim_avatar = itemView.findViewById(R.id.civ_avatar);
            tv_from_nickname = itemView.findViewById(R.id.tv_from_nickname);
            tv_apply_date = itemView.findViewById(R.id.tv_apply_date);
            tv_from_username = itemView.findViewById(R.id.tv_from_username);
            tv_status = itemView.findViewById(R.id.tv_status);
            tv_check_through = itemView.findViewById(R.id.tv_check_through);
            tv_check_reject = itemView.findViewById(R.id.tv_check_reject);
            ll_status = itemView.findViewById(R.id.ll_status);
            ll_check = itemView.findViewById(R.id.ll_check);
        }
    }
}
